<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\User;
use Illuminate\Support\Facades\Redirect;
use Qiniu\Storage;


class UploadController extends Controller
{
    public function uploadpicture(Request $request)
    {
        $this->validate(request(), [
            'file' => 'required|image',
        ]);
        if ($request->hasFile('file') && $request->file('file')->isValid()) {
            $path = md5(time() . rand(100000, 999999)) .'.'. $request->file('file')->extension();
            $img = 'http://picture.105577.xyz/'.$path;
            $res = \Storage::disk('qiniu')->writeStream($path, fopen($request->file('file'), 'r'));
            if ($res) {
                return $img;
            } else {
                return '图片上传错误';
            }
        }
    }

    public function uploadYoupaiPicture(Request $request)
    {
        $this->validate(request(), [
            'file' => 'required|image',
        ]);
        if ($request->hasFile('file') && $request->file('file')->isValid()) {
            $sha1 = sha1_file($request->file('file'));
            $md5 = md5_file($request->file('file'));
            if ($file_url = \Redis::get($md5)){
                if ($file_url1 = \Redis::get($sha1)){
                    if ($file_url == $file_url1){
                        if ($file_url == 'shield')
                            return 'shield';
                        return 'https://'.env('UPYUN_DOMAIN').'/'.$file_url;
                    }
                }
            }
            $path = md5(time() . rand(100000, 999999)) .'.'. $request->file('file')->extension();
            $img = 'https://'.env('UPYUN_DOMAIN').'/'.$path;
            $res = \Storage::disk('upyun')->writeStream($path, fopen($request->file('file'), 'r'));
            if ($res) {
                \Redis::set($md5,$path);
                \Redis::set($sha1,$path);
                \Redis::set($path,$md5.'/'.$sha1);
                return $img;
            } else {
                return '图片上传错误';
            }
        }
    }
}
